<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单详情号" prop="detailId">
        <el-input
          v-model="queryParams.detailId"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后类型" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.refund_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.refund_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column
        label="订单号"
        align="center"
        prop="orderId"
        width="200"
      />
      <el-table-column
        label="商品名称"
        align="center"
        prop="productName"
        width="200"
      />
      <el-table-column
        label="商品图片"
        align="center"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.productImg"
            :preview-src-list="[scope.row.productImg]"
            style="width: 60px; height: 60px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="售后类型" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.refund_type"
            :value="scope.row.refundType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="申请原因"
        align="center"
        prop="comment"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="回寄单号"
        align="center"
        prop="returnSlip"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="发货单号"
        align="center"
        prop="dispatchNote"
        min-width="150"
      />
      <el-table-column
        label="退款时间"
        align="center"
        prop="refundTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="售后金额"
        align="center"
        prop="money"
        min-width="150"
      />
      <el-table-column
        label="审核备注"
        align="center"
        prop="remark"
        width="200"
      />
      <el-table-column label="售后状态" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.refund_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
        fixed="right"
      >
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleDetail(scope.row)"
              >详情</el-button
            >
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status == 1"
              @click="handleExamine(scope.row)"
              >审核</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :before-close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="status" required>
          <el-radio-group v-model="form.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="6">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="回寄单号"
          v-if="form.refundType == 3 && form.status == 2"
          prop="returnSlip"
        >
          <el-input
            v-model="form.returnSlip"
            type="text"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="驳回原因" v-if="form.status == 6">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 售后详情 -->
    <el-dialog
      title="售后详情"
      :visible.sync="openDetail"
      width="700px"
      append-to-body
      :before-close="close"
    >
      <el-form ref="form" :model="formDetail" label-width="120px">
        <el-form-item label="订单号">
          <el-input :value="formDetail.orderId" readonly />
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input :value="formDetail.productName" readonly />
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input :value="formDetail.productName" readonly />
        </el-form-item>
        <el-form-item label="商品图片">
          <el-input :value="formDetail.productImg" readonly />
        </el-form-item>
        <el-form-item label="售后类型">
          <dict-tag
            :options="dict.type.refund_type"
            :value="formDetail.refundType"
          />
        </el-form-item>
        <el-form-item label="申请时间">
          <el-input :value="parseTime(formDetail.createTime)" readonly />
        </el-form-item>
        <el-form-item label="回寄单号">
          <el-input :value="formDetail.returnSlip" readonly />
        </el-form-item>
        <el-form-item label="发货单号">
          <el-input :value="formDetail.dispatchNote" readonly />
        </el-form-item>
        <el-form-item label="退款时间">
          <el-input :value="parseTime(formDetail.refundTime)" readonly />
        </el-form-item>
        <el-form-item label="售后金额">
          <el-input :value="formDetail.money" readonly />
        </el-form-item>
        <el-form-item label="审核备注">
          <el-input :value="formDetail.remark" readonly />
        </el-form-item>
        <el-form-item label="售后状态">
          <dict-tag
            :options="dict.type.refund_status"
            :value="formDetail.status"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openDetail = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Debounce } from "@/utils/tools.js";
import { getToken } from "@/utils/auth";
import {
  orderList,
  afterSaleRefundAudit,
} from "@/api/afterSaleManagement/afterSaleList";
import store from "@/store";
export default {
  name: "orderList",
  dicts: ["pay_type", "order_status", "refund_type", "refund_status"],
  data() {
    return {
      openAdd: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "售后审核",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderId: "",
        orderStatus: "",
      },
      // 表单参数
      formAdd: {},
      // 表单参数
      form: {
        refundId: null, //售后id
        status: null, //售后状态 1 申请中 2 退款完成 3 待收货 4 商家收货 5 商家发货 6 审核驳回
        returnSlip: null, //回寄单号
        remark: null, //审核备注
      },
      // 表单校验
      rules: {
        deliverNo: [
          { required: true, message: "请输入物流单号", trigger: "blur" },
        ],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        num: [{ required: true, message: "请输入数量", trigger: "blur" }],
        deliveryOrNot: [
          { required: true, message: "请选择是否发货", trigger: "change" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
        deliverType: [
          { required: true, message: "请选择发货方式", trigger: "change" },
        ],
        returnSlip: [
          { required: true, message: "请输入回寄单号", trigger: "blur" },
        ],
      },
      deliverName: "",
      deliverTypeList: [
        { label: "物流", value: 1 },
        { label: "自提", value: 2 },
      ],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/order/batchDeliver",
      },
      formDetail: {},
      openDetail: false,
      roles: true,
    };
  },
  created() {
    let roles = store.getters && store.getters.roles;
    this.roles = roles.indexOf("admin") === -1 ? false : true;
    this.getList();
  },
  methods: {
    // 复制地址
    copy(formDetail) {
      let oInput;
      if (document.getElementById("copyInput")) {
        oInput = document.getElementById("copyInput");
      } else {
        oInput = document.createElement("input");
      }
      oInput.value =
        formDetail.receiveProvince +
        formDetail.receiveCity +
        formDetail.receiveArea +
        formDetail.receiveAddress;
      oInput.id = "copyInput";
      oInput.className = "copy-input";
      document.body.appendChild(oInput);
      oInput.select(); // 选择对象;
      document.execCommand("Copy"); // 执行浏览器复制命令
      this.$message.success("复制成功！");
    },
    // 售后详情
    handleDetail(data) {
      this.formDetail = data;
      this.openDetail = true;
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      orderList(this.queryParams).then((response) => {
        this.orderList = response.data;
        this.total = response.extend.total;
        this.loading = false;
      });
    },
    // return this.$modal.msgWarning("请输入物流单号");
    // 取消按钮
    cancel() {
      this.open = false;
      this.openAdd = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.deliverName = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 售后审核 */
    handleExamine(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.form.status = null;
      this.open = true;
    },
    close() {
      this.reset();
      this.form = {};
      this.open = false;
      this.formDetail = {};
      this.openDetail = false;
    },
    /** 提交按钮 */
    submitForm: Debounce(function (e) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.status == 6) {
            this.form.returnSlip = null;
          }
          let data = {
            refundId: this.form.refundId, //售后id
            status: this.form.status, //售后状态 1 申请中 2 退款完成 3 待收货 4 商家收货 5 商家发货 6 审核驳回
            returnSlip: this.form.returnSlip, //回寄单号
            remark: this.form.remark, //审核备注
          };
          afterSaleRefundAudit(data).then((res) => {
            this.$modal.msgSuccess("审核操作成功");
            this.open = false;
            this.form = {
              refundId: null, //售后id
              status: null, //售后状态 1 申请中 2 退款完成 3 待收货 4 商家收货 5 商家发货 6 审核驳回
              returnSlip: null, //回寄单号
              remark: null, //审核备注
            };
            this.getList();
          });
        }
      });
    }, 500),
  },
};
</script>
<style scoped lang="scss">
.flexBox {
  display: flex;
  justify-content: space-between;
}
.flex-1 {
  flex: 1;
}
</style>
